package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.Exceptions.CheckItemException;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckItemService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @outhor
 * @date 2020/1/10 14:14
 */
@RequestMapping("checkitem")
@RestController
public class CheckItemController {
    //服务发现和注册
    //负载均衡（随机，轮训，一致性hash，最少活跃数）
    //集群容错策略（failover，根据你配置重试次数）
    //    cluster =  "failover"
    //    cluster =  "failfast"  失败了就失败了
    @Reference(cluster = "failfast")
    private CheckItemService checkItemService;

    @RequestMapping("add")
    public Result CheckItemAdd(@RequestBody CheckItem checkItem) {
        try {
            checkItemService.CheckItemAdd(checkItem);
            return new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("findPage")
    public Result findPage(@RequestBody QueryPageBean queryPageBean) {
        try {
            PageResult page = checkItemService.findPage(queryPageBean);
            return new Result(true,MessageConstant.QUERY_CHECKITEM_SUCCESS,page);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.QUERY_CHECKITEM_FAIL);
        }

    }

    @RequestMapping("updata")
    public Result CheckItemUpdata(@RequestBody CheckItem checkItem){
        try {
            checkItemService.CheckItemUpdata(checkItem);
            return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return  new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
    }
    @RequestMapping("delete")
    @PreAuthorize("hasAuthority('CHECKITEM_DELETE')")
    public Result CheckItemDelete(Integer id){
        try {
            checkItemService.CheckItemDelete(id);
            return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
        }catch (CheckItemException e){
            return new Result(false,e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKITEM_FAIL);
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        CheckItem checkItem = checkItemService.findById(id);
        return new Result(true,"",checkItem);
    }

    @RequestMapping("checkItemFindAll")
    public Result checkItemFindAll(){
        List<CheckItem> checkItemList = checkItemService.checkItemFindAll();
        return new Result(true,"",checkItemList);
    }
}
